package com.eis.persist.dao.util;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;

import com.eis.persist.model.function.PersistFunctionParameter;

public class FunctionParameterBatchSetter implements BatchPreparedStatementSetter {
	private List<PersistFunctionParameter> parameters;
	
	public FunctionParameterBatchSetter(List<PersistFunctionParameter> parameters) {
		this.parameters = parameters;
	}

	@Override
	public void setValues(PreparedStatement ps, int i) throws SQLException {
		PersistFunctionParameter p = parameters.get(i);
		ps.setInt(1, p.getFunctionDBID());
		ps.setString(2, p.getParameterXMl());

		if (p.getId() > 0) {
			ps.setTimestamp(3, new Timestamp(p.getUpdateDate().getTime()));
			ps.setString(4, p.getUpdatedBy());
			ps.setInt(5, p.getId());
		} else {
			ps.setTimestamp(3, new Timestamp(p.getCreateDate().getTime()));
			ps.setString(4, p.getCreatedBy());
		}
	}

	@Override
	public int getBatchSize() {
		if (parameters != null) {
			return parameters.size();
		} else {
			return 0;
		}
	}

}
